package com.xxx.meter.model.utils;

import android.os.Handler;
import android.os.Message;

import com.xxx.meter.R;
import com.xxx.meter.app.AppManager;

public class ExitAppUtil {

    private static final Object SYC = new Object();
    private static ExitAppUtil util;

    private MyHandler mHandler;

    private ExitAppUtil() {
        mHandler = new MyHandler();
    }

    public static ExitAppUtil getInstance() {
        if (util == null) {
            synchronized (SYC) {
                if (util == null) {
                    util = new ExitAppUtil();
                }
            }
        }
        return util;
    }

    //二次点击
    private static class MyHandler extends Handler {

        boolean backCount;

        //此处不可使用 弱引用对象
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                backCount = false;
            }
        }
    }

    public void onBackPressed() {
        if (mHandler.backCount) {
            mHandler.removeMessages(1);
            AppManager.getInstance().AppExit();
            return;
        }
        mHandler.backCount = true;
        mHandler.sendEmptyMessageDelayed(1, 1500);
        ToastUtil.showToast(R.string.second_exit_app);
    }

}